/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using MyPhotoIndex.Controls;
using System.Drawing;
using MyPhotoIndex.Interfaces;
using MyPhotoIndex.Controls.TagTree;
using MyPhotoIndex.ImageBank;
using MyPhotoIndex.Utilities;
using MyPhotoIndex.Interfaces.Tagging;

namespace MyPhotoIndex
{
    partial class Main
    {
        private ToolTip m_tagsPalettetooltip = new ToolTip();
        private SelectedTagsCollection m_tags;
        private MyPhotoIndexThumbnailViewer m_source;

        private void InitializeTagsPalette()
        {
            m_tags = new SelectedTagsCollection();

            tagsPaletteThumbnailViewer.ImageList = m_tags;
            tagsPaletteThumbnailViewer.DragDrop += new DragEventHandler(tagsPaletteThumbnailViewer_DragDrop);
            tagsPaletteThumbnailViewer.DragOver += new DragEventHandler(tagsPaletteThumbnailViewer_DragOver);
            tagsPaletteThumbnailViewer.DragEnter += new DragEventHandler(tagsPaletteThumbnailViewer_DragEnter);
            tagsPaletteThumbnailViewer.DragLeave += new EventHandler(tagsPaletteThumbnailViewer_DragLeave);
            tagsPaletteThumbnailViewer.MouseDown += new MouseEventHandler(tagsPaletteThumbnailViewer_MouseDown);
            tagsPaletteThumbnailViewer.MouseMove += new MouseEventHandler(tagsPaletteThumbnailViewer_MouseMove);
            tagsPaletteThumbnailViewer.MouseLeave += new EventHandler(tagsPaletteThumbnailViewer_MouseLeave);
        }

        public void RefreshTagsPalette()
        {
            m_tags.Refresh();
            tagsPaletteThumbnailViewer.Refresh();
        }

        private void tagsPaletteThumbnailViewer_MouseLeave(object sender, EventArgs e)
        {
            m_tagsPalettetooltip.Hide(tagsPaletteThumbnailViewer);
        }

        private void tagsPaletteThumbnailViewer_MouseMove(object sender, MouseEventArgs e)
        {
            MyPhotoIndexThumbnailViewer viewer = sender as MyPhotoIndexThumbnailViewer;
            Thumbnail thumbnail = viewer.GetThumbnailFromPosition(Cursor.Position);
            if (thumbnail != null)
            {
                //m_tooltip.Hide(viewer as IWin32Window);

                Point pos = viewer.PointToClient(Cursor.Position);
                pos.X += 15;
                pos.Y += 5;
                m_tagsPalettetooltip.Show(ImageTagCollection.Instance[thumbnail.ImageData.Id].Path, viewer, pos);
            }
            else
            {
                m_tagsPalettetooltip.Hide(viewer);
            }
        }

        private void tagsPaletteThumbnailViewer_MouseDown(object sender, MouseEventArgs e)
        {
            m_source = sender as MyPhotoIndexThumbnailViewer;
        }

        private void tagsPaletteThumbnailViewer_DragLeave(object sender, EventArgs e)
        {
            MyPhotoIndexThumbnailViewer tv = sender as MyPhotoIndexThumbnailViewer;
            Win32Interop.ImageDragHelper.ImageList_DragLeave(tv.Handle);
        }

        private void tagsPaletteThumbnailViewer_DragEnter(object sender, DragEventArgs e)
        {
            MyPhotoIndexThumbnailViewer tv = sender as MyPhotoIndexThumbnailViewer;
            Win32Interop.ImageDragHelper.ImageList_DragEnter(tv.Handle, e.X - tv.Left, e.Y - tv.Top);
        }

        private void tagsPaletteThumbnailViewer_DragOver(object sender, DragEventArgs e)
        {
            MyPhotoIndexThumbnailViewer tv = sender as MyPhotoIndexThumbnailViewer;
            // Compute drag position and move image
            Point point = tv.PointToClient(new Point(e.X, e.Y));
            Win32Interop.ImageDragHelper.ImageList_DragMove(point.X, point.Y);

            TreeNode tn = (TreeNode)e.Data.GetData(typeof(TreeNode));
            IImageTag imageTag = null;
            if (tn == null)
            {
                Thumbnail thumbnail = (Thumbnail)e.Data.GetData(typeof(Thumbnail));
                if (thumbnail != null)
                {
                    if (thumbnail.ImageData is TagsImageData)
                    {
                        ImageTagCollection tagFactory = ImageTagCollection.Instance;
                        imageTag = tagFactory[thumbnail.ImageData.Id];
                    }
                    else
                        if (thumbnail.ImageData is ImageData)
                        {
                            if (thumbnail.ImageData.Tags.Length > 0)
                            {
                                ImageTagCollection tagFactory = ImageTagCollection.Instance;
                                imageTag = tagFactory[thumbnail.ImageData.Tags[0]];
                            }
                        }
                }
            }
            else
            {
                imageTag = tn.Tag as IImageTag;
            }

            if (imageTag != null &&
                imageTag.Parent != null)
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void tagsPaletteThumbnailViewer_DragDrop(object sender, DragEventArgs e)
        {
            TreeNode selectedtagNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
            Thumbnail thumbnail = null;
            if (selectedtagNode == null)
            {
                thumbnail = (Thumbnail)e.Data.GetData(typeof(Thumbnail));
            }

            if (selectedtagNode == null &&
                thumbnail == null)
            {
                return;
            }

            IImageTag tag = null;
            List<int> tags = new List<int>();


            if (selectedtagNode != null &&
                myPhotoIndexImageTagTreeView1.SelectedNodes.Contains(selectedtagNode) == true)
            {
                foreach (TreeNode node in myPhotoIndexImageTagTreeView1.SelectedNodes)
                {
                    if (node.Parent != null)
                    {
                        int tagId = Convert.ToInt32(node.Name, System.Globalization.CultureInfo.InvariantCulture);
                        tags.Add(tagId);
                    }
                }
            }
            if (selectedtagNode != null)
            {
                tag = selectedtagNode.Tag as IImageTag;
                tags.Add(tag.Id);
            }
            else
                if (thumbnail != null)
                {
                    if (thumbnail.ImageData is TagsImageData)
                    {
                        tag = ImageTagCollection.Instance[thumbnail.ImageData.Id];
                        if (m_source != null)
                        {
                            m_source = null;
                            SelectedTagsCollection collection = m_source.ImageList as SelectedTagsCollection;
                            collection.Remove(tag.Id);
                            m_source.Refresh();
                        }
                        tags.Add(tag.Id);
                    }
                    else
                        if (thumbnail.ImageData is ImageData)
                        {
                            tags.AddRange(thumbnail.ImageData.Tags);
                        }
                }

            if (tags.Count == 0)
            {
                return;
            }

            MyPhotoIndexThumbnailViewer tv = sender as MyPhotoIndexThumbnailViewer;
            SelectedTagsCollection tagsCollection = tv.ImageList as SelectedTagsCollection;
            foreach (int tagId in tags)
            {
                tagsCollection.Add(tagId);
            }

            tv.Refresh();

            AdjustSelectedImagesPane();
        }

        private void ClearTagsPalette()
        {
            m_tags.Clear();
            tagsPaletteThumbnailViewer.Refresh();
            clearTagsPaletteToolStripButton.Enabled = (tagsPaletteThumbnailViewer.ImageList != null) ? (tagsPaletteThumbnailViewer.ImageList.Count > 0) : false;
        }
    }
}
